/*
 *  Copyright (c) 2008 Howard Shank
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), to deal in 
 * the Software without restriction, including without limitation the rights to use, 
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
 * Software, and to permit persons to whom the Software is furnished to do so, subject 
 * to the following conditions:

 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

Type.registerNamespace('AjaxCtrlsExtenders');

AjaxCtrlsExtenders.DisableButtonBehavior = function(element) {
    /// <summary>
    /// This extender can be attached to a button to allow the button to be automatically
    /// disabled as soon as it is clicked. Helps prevent multiple submission of web forms
    /// where only one click is desired.
    /// </summary>
    
    AjaxCtrlsExtenders.DisableButtonBehavior.initializeBase(this, [element]);

    // Properties from Class file
    this._DisabledText = null;
    
    // local values
    this._oldValue = null;
    this._onkeyupDelegate = null;
    this._onclickDelegate = null;
}

AjaxCtrlsExtenders.DisableButtonBehavior.prototype = {

    initialize : function() {
        AjaxCtrlsExtenders.DisableButtonBehavior.callBaseMethod(this, 'initialize');

        if(this._onclickDelegate == null)
        {
            this._onclickDelegate = Function.createDelegate(this, this._onclick);
        }
        $addHandler(this.get_element(), 'click', this._onclickDelegate);
     },

    dispose : function() {
        if(this._onclickDelegate)
        {
            $removeHandler(this.get_element(), 'click', this._onclickDelegate);
            delete this._onclickDelegate;
        }
        AjaxCtrlsExtenders.DisableButtonBehavior.callBaseMethod(this, 'dispose');
    },

     get_DisabledText : function() {
        return this._DisabledText;
    },

    set_DisabledText : function(value) {
        this._DisabledText = value;
    },
    
    _onclick : function() {
        var disabled = (this._DisabledText == this._element.value);
        this.disabled = disabled;
        if (!disabled) {
            if(this._DisabledText && this._DisabledText != "") {
                //Sys.Debug.trace("changing text");
                this._oldValue = this._element.value;
                this._element.value = this._DisabledText;
            }
            this._element.disabled = true;
        } else {
            this._element.disabled = false;
            if (this._oldValue) {
                //Sys.Debug.trace("setting to old value " + this._oldValue);
                this._element.value = this._oldValue;
            }
        }
    }
}

AjaxCtrlsExtenders.DisableButtonBehavior.registerClass('AjaxCtrlsExtenders.DisableButtonBehavior', AjaxControlToolkit.BehaviorBase);
